{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b24c4d20",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "161ff646",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1,) (2,) (3,)]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([(1,), (2,), (3,)], dtype=[('age', 'i1')])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = np.dtype([('age', np.int8)])\n",
    "a = np.array([1,2,3], dtype=dt)\n",
    "print(a)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f86c51f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调整数组大小\n",
    "a = np.array([[1,2,3],[4,5,6]]).reshape(3,2)\n",
    "print(type(a))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4b703fc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.itemsize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f1f1b593",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2]\n",
      "4\n",
      "[1 2]\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 返回数组中每个字节单位长度\n",
    "a = np.array([1,2], dtype='i1')\n",
    "# 如果不指定type，元素默认是4个字节\n",
    "b = np.array([1,2])\n",
    "print(b)\n",
    "print(b.itemsize)\n",
    "print(a)\n",
    "print(a.itemsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b2b0835f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  C_CONTIGUOUS : True\n",
       "  F_CONTIGUOUS : True\n",
       "  OWNDATA : True\n",
       "  WRITEABLE : True\n",
       "  ALIGNED : True\n",
       "  WRITEBACKIFCOPY : False\n",
       "  UPDATEIFCOPY : False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flags\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c827470",
   "metadata": {},
   "source": [
    "# 数组与元素操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6030181f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  5, 10, 15],\n",
       "       [20, 25, 30, 35],\n",
       "       [40, 45, 50, 55]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数值迭代\n",
    "a = np.arange(0,60,5).reshape(3,4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ca45c62a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "打印出0维数组: 0\n",
      "打印出0维数组: 5\n",
      "打印出0维数组: 10\n",
      "打印出0维数组: 15\n",
      "打印出0维数组: 20\n",
      "打印出0维数组: 25\n",
      "打印出0维数组: 30\n",
      "打印出0维数组: 35\n",
      "打印出0维数组: 40\n",
      "打印出0维数组: 45\n",
      "打印出0维数组: 50\n",
      "打印出0维数组: 55\n",
      "打印出一维数组: [ 0 20 40]\n",
      "打印出一维数组: [ 5 25 45]\n",
      "打印出一维数组: [10 30 50]\n",
      "打印出一维数组: [15 35 55]\n"
     ]
    }
   ],
   "source": [
    "for x in np.nditer(a): \n",
    "    print('打印出0维数组:',x)\n",
    "for x in np.nditer(a, flags=['external_loop'], order='F'):\n",
    "    print('打印出一维数组:', x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "045ac054",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0 20 40]\n",
      " [ 5 25 45]\n",
      " [10 30 50]\n",
      " [15 35 55]]\n"
     ]
    }
   ],
   "source": [
    "# 转置\n",
    "print(a.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2c0a0aa5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  5 10 15]\n",
      " [20 25 30 35]\n",
      " [40 45 50 55]]\n",
      "[1 2 3 4]\n",
      "0: 1\n",
      "5: 2\n",
      "10: 3\n",
      "15: 4\n",
      "20: 1\n",
      "25: 2\n",
      "30: 3\n",
      "35: 4\n",
      "40: 1\n",
      "45: 2\n",
      "50: 3\n",
      "55: 4\n"
     ]
    }
   ],
   "source": [
    "# 对可以广播的两个数组进行同时迭代\n",
    "a = np.arange(0,60,5).reshape(3,4)\n",
    "print(a)\n",
    "b = np.array([1,2,3,4], dtype=int)\n",
    "print(b)\n",
    "for x,y in np.nditer([a,b]):\n",
    "    print('%d: %d' %(x,y))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff615b83",
   "metadata": {},
   "source": [
    "# 数组变形\n",
    "- reshape\n",
    "- flat:数组上的一维迭代器\n",
    "- flatten:返回折叠为一维的数组副本\n",
    "- ravel: 返回连续展开数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a517304a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1,9).reshape(4,2)\n",
    "print(a)\n",
    "# 返回展开数组对应的下标元素\n",
    "a.flat[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "31667211",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 'C'为按行，'F'为按列，'A'为原顺序，'k'为元素在内存中出现的顺序。\n",
    "a.flatten(order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d2eddaa0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flatten(order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "de2f51bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flatten(order='A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "cb92c3e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel(order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a2ffe7f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3]],\n",
       "\n",
       "       [[4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "a=np.arange(8).reshape(2,2,2)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a217993",
   "metadata": {},
   "source": [
    "# 向后滚动特定的轴，直到个特定的位置\n",
    "rollaxis(arr, axis, start)\n",
    "- axis: 要向后滚动的轴，其他轴的相对位置不会改变\n",
    "- start: 默认为0，表示完整的滚动，会滚动到特定位置\n",
    "\n",
    "利用索引理解下方代码\n",
    "上面数组的索引为\n",
    "000 001\n",
    "010 011\n",
    "100 101\n",
    "110 111\n",
    "按顺序一次理解为0,1,2轴\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "94e1779a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0 2]\n",
      "  [4 6]]\n",
      "\n",
      " [[1 3]\n",
      "  [5 7]]]\n",
      "[[[0 2]\n",
      "  [1 3]]\n",
      "\n",
      " [[4 6]\n",
      "  [5 7]]]\n",
      "[[[0 1]\n",
      "  [4 5]]\n",
      "\n",
      " [[2 3]\n",
      "  [6 7]]]\n",
      "[[[0 1]\n",
      "  [2 3]]\n",
      "\n",
      " [[4 5]\n",
      "  [6 7]]]\n",
      "[[[0 1]\n",
      "  [2 3]]\n",
      "\n",
      " [[4 5]\n",
      "  [6 7]]]\n"
     ]
    }
   ],
   "source": [
    "# 将轴2滚动到轴0(宽度到深度)\n",
    "# 0,1,2 -> 1,2,0\n",
    "print(np.rollaxis(a,2))\n",
    "# 将轴2滚动到轴1(宽度到高度)\n",
    "# 0,1,2 -> 0,2,1\n",
    "print(np.rollaxis(a,2,1))\n",
    "# 0,1,2 -> 1,0,2\n",
    "print(np.rollaxis(a,1))\n",
    "\n",
    "# 下面两个不变\n",
    "print(np.rollaxis(a,1,2))\n",
    "print(np.rollaxis(a,0,1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77161209",
   "metadata": {},
   "source": [
    "# swapaxes(arr, axis1, axis2)\n",
    "- axis1 对应的第一个轴\n",
    "- axis2 对应的第二个轴"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "93c2ad52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 2],\n",
       "        [1, 3]],\n",
       "\n",
       "       [[4, 6],\n",
       "        [5, 7]]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0,1,2 -》 0，2,1\n",
    "np.swapaxes(a, 2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "90cdd598",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.21.5'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fd147af6",
   "metadata": {},
   "source": [
    "# 添加、删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "a94132c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "b: [[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "c: [[1 2 3]\n",
      " [4 5 6]\n",
      " [1 2 3]]\n"
     ]
    }
   ],
   "source": [
    "# 修改数组\n",
    "a= np.array([[1,2,3],[4,5,6]])\n",
    "b = np.resize(a, (3,2))\n",
    "c=np.resize(a, (3,3))\n",
    "print(a)\n",
    "print('b:',b)\n",
    "# 不够则添加重复数据补充\n",
    "print('c:',c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "15ae8fbf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "向数组添加元素 [1 2 3 4 5 6 7 8 9]\n",
      "沿轴0添加元素 [[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "沿轴1添加元素 [[1 2 3 7 8 9]\n",
      " [4 5 6 2 2 2]]\n"
     ]
    }
   ],
   "source": [
    "# append(arr, values, axis) 在数组末尾添加值\n",
    "a= np.array([[1,2,3],[4,5,6]])\n",
    "b = np.append(a, [7,8,9])\n",
    "c=np.append(a,[[7,8,9]],axis=0)\n",
    "d=np.append(a,[[7,8,9],[2,2,2]],axis=1)\n",
    "print(a)\n",
    "print('向数组添加元素',b)\n",
    "print('沿轴0添加元素',c)\n",
    "print('沿轴1添加元素',d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e49b588",
   "metadata": {},
   "source": [
    "# 字符串连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "1ad64f9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array('hello hello ', dtype='<U12')"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.char.multiply('hello ', 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "4b012589",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array('*******hello********', dtype='<U20')"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.char.center('hello', 20, fillchar='*')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "cd8cb54d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array('Hello world', dtype='<U11')"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.char.capitalize('hello world')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9612bed7",
   "metadata": {},
   "source": [
    "# 统计函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b898709",
   "metadata": {},
   "source": [
    "- np.amin()\n",
    "- np.amax()\n",
    "- np.ptp()返回沿轴值的范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "443e187b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 5 7]\n",
      " [8 4 3]\n",
      " [1 4 9]]\n",
      "8\n",
      "[7 1 6]\n",
      "[4 5 8]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[3,5,7],[8,4,3],[1,4,9]])\n",
    "print(a)\n",
    "print(np.ptp(a))\n",
    "print(np.ptp(a, axis=0))\n",
    "print(np.ptp(a, axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0df7d4bc",
   "metadata": {},
   "source": [
    "# np.percentile(a, q, axis)\n",
    "分位数计算指的是对数据排序后再找到该分位的值\n",
    "\n",
    "- q: 要计算的百分位数，在0~100之间\n",
    "- axis: 轴"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "be071724",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[30 40 70]\n",
      " [80 20 10]\n",
      " [50 90 60]]\n",
      "50.0\n",
      "50.0\n",
      "[40. 20. 60.]\n",
      "[50. 40. 60.]\n",
      "666.6666666666666\n",
      "666.6666666666666\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[30,40,70],[80,20,10],[50,90,60]])\n",
    "print(a)\n",
    "# 50 ，即二分位数，中间数\n",
    "base = 50\n",
    "print(np.percentile(a,base))\n",
    "print(np.median(a))\n",
    "print(np.percentile(a,base, axis=1))\n",
    "print(np.percentile(a,base, axis=0))\n",
    "\n",
    "# 标准差\n",
    "print(np.std(a))\n",
    "# 方差\n",
    "print(np.var(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85dddc93",
   "metadata": {},
   "source": [
    "# 排序，搜索，和计数函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa70dc0d",
   "metadata": {},
   "source": [
    "# np.sort(a, axis, kind, order)\n",
    "- kind: 默认quicksort  (mergesort,归并排序)(heapsort,堆排序)\n",
    "- order: 如果包含字段，就是要排序的字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "c9209919",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(b'aghs', 12) (b'hikd', 22) (b'sdff', 33)]\n"
     ]
    }
   ],
   "source": [
    "dt =np.dtype([('name', 'S10'), ('age', 'i1')])\n",
    "a = np.array([('aghs', 12), ('hikd',22), ('sdff', 33)],dtype=dt)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "df72cf43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([(b'aghs', 12), (b'hikd', 22), (b'sdff', 33)],\n",
       "      dtype=[('name', 'S10'), ('age', 'i1')])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(a, order='name')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11174b83",
   "metadata": {},
   "source": [
    "# np.argsort(),返回排序后的索引值，用于重构排序后的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "a9583e35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 1 2]\n",
      "[1 2 0]\n",
      "[1 2 3]\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "x = np.array([3,1,2])\n",
    "print(x)\n",
    "y = np.argsort(x)\n",
    "print(y)\n",
    "print(x[y])\n",
    "\n",
    "for i in y:\n",
    "    print(x[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf0ce55c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
